package com.sifeng.test;

import com.sifeng.enums.FileTypeEnum;
import com.sifeng.pojo.WorkloadRatioDTO;
import com.sifeng.utils.MyUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang_gsh on 2018/12/7.
 * 填写每月开发工作量的单元数量
 */
public class UnitNumberOfMonthlyDevelopmentWorkload {
	private static String basePath = "E:\\hisun\\开发三部\\功能点评估表\\";
	private static String readCurPath = basePath + "201903\\";
	private static String writeFilePath = readCurPath + "2019.03需求开发管理开发报表.xlsx";  //填写开发工作量的文档
	//private static String leadDepartment = "营销服务研发部";   //需要填写的主导部门
	private static int cellReqNumIndex = 0;  //需求编号列
	private static int writeColumn = 2;  //填写单元数量的列

	private static int readRow = 2;
	private static int readColumn = 9;

	public static Map<String, WorkloadRatioDTO> getWorkloadRatio(String path) {
		Map<String, WorkloadRatioDTO> map = new HashMap<String, WorkloadRatioDTO>();
		List<String> dirList = MyUtils.readDirectory(path, FileTypeEnum.DIRECTORY);

		for (String s : dirList) {
			WorkloadRatioDTO workloadRatioDTO = new WorkloadRatioDTO();
			String[] str = s.split("_");
			List<String> fileList = MyUtils.searchFile(path + s, str[0], "_原子功能点", ".xlsx");
			if(fileList.size() == 0) {
				System.out.println(str[0] + "_" + str[1] + "：原子功能点未找到！");
				continue;
			}
			workloadRatioDTO.setRequirementNumber(str[0]);
			workloadRatioDTO.setRequirementName(str[1]);
			double unitNumber = getUnitNumber(path + s + "\\" + fileList.get(0));
			workloadRatioDTO.setUnitNumer((new Double(unitNumber)).intValue());
			map.put(str[0], workloadRatioDTO);
		}
		return map;
	}

	public static double getUnitNumber(String excelPath) {
		InputStream iStream=null;
		XSSFWorkbook wb = null;
		double unitNumber = -1;
		try {
			iStream=new FileInputStream(excelPath);
			wb = new XSSFWorkbook(iStream);
		} catch (IOException e) {
			e.printStackTrace();
			return unitNumber;
		}
		XSSFSheet sheet = wb.getSheetAt(0);
		if (sheet == null) {
			return unitNumber;
		}

		XSSFRow row_2 = sheet.getRow(readRow);
		XSSFCell cell_2J = row_2.getCell(readColumn);

		unitNumber = cell_2J.getNumericCellValue();

		return unitNumber;
	}

	public static void writeExcel(Map<String, WorkloadRatioDTO> map) throws IOException {
		XSSFWorkbook wb = MyUtils.getXSSFWorkbook(writeFilePath);
		XSSFSheet sheet = wb.getSheetAt(0);
		if (sheet == null) {
			return;
		}

		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			XSSFRow row = sheet.getRow(i);
			if(row == null) {
				continue;
			}

			XSSFCell cellReqNum = row.getCell(cellReqNumIndex);
			if(cellReqNum == null) {
				continue;
			}
			String reqNum = cellReqNum.getStringCellValue();
			/*if(!leadDepartment.equals(leadDepartmentTmp.trim())) {
				continue;
			}*/
			if(!map.containsKey(reqNum)) {
				continue;
			}


			/*XSSFCell cellReqNum = row.getCell(2);
			String reqNum = cellReqNum.getStringCellValue();*/

			XSSFCell cellWrite = row.getCell(writeColumn);
			System.out.println(reqNum + ":" + map.get(reqNum).getUnitNumer());
			cellWrite.setCellValue(map.get(reqNum).getUnitNumer());
		}

		FileOutputStream outputStream = new FileOutputStream(writeFilePath);

		wb.write(outputStream);
		outputStream.flush();
		wb.close();
	}

	public static void main(String[] args) throws IOException {
		String path = "E:\\hisun\\开发三部\\功能点评估表\\201903\\3月开发复评材料-杨贵生\\";
		Map<String, WorkloadRatioDTO> map = UnitNumberOfMonthlyDevelopmentWorkload.getWorkloadRatio(path);
		UnitNumberOfMonthlyDevelopmentWorkload.writeExcel(map);
	}
}
